package com.hejing.异常处理方式2_throws抛出异常;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * ClassName: ThrowsTest
 * Package: com.hejing.异常处理方式2_throws抛出异常
 * Description:
 *
 * @Author: HeJing
 * @Create: 2023-04-11 9:41
 * @Version: 1.0
 */
public class ThrowsTest {

    /**
     * 可以进行异常处理，
     */
    public void method03() {
        try {
            method02();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            System.out.println("异常处理结束");
        }
    }

    /**
     * 处理不了，将异常抛出去
     *
     * @throws FileNotFoundException
     * @throws IOException
     */
    public void method02() throws FileNotFoundException, IOException {
        method01();
    }

    /**
     * 做不了，将异常抛出去
     *
     * @throws FileNotFoundException
     * @throws IOException
     */
    public void method01() throws FileNotFoundException, IOException {

        File file = new File("D:\\HeJing_Java\\test\\hello.txt");
        FileInputStream fis = new FileInputStream(file);  // 可能报 FileNotFoundException异常

        int data = fis.read();  // 可能报 IOException 异常
        while (data != -1) {
            System.out.print((char) data);
            data = fis.read();  // 可能报 IOException 异常
        }
        fis.close();  // 可能报 IOException 异常
    }

    public static void main(String[] args) {
        // method01();
        ThrowsTest test = new ThrowsTest();
        test.method03();
    }
}































